/*
Copyright (c) 2010, Jonathan Baldwin
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
  list of conditions and the following disclaimer in the documentation and/or
  other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef RECAP_SCREENCAPTURE_H
#define RECAP_SCREENCAPTURE_H
#include "RecapUtil.h"
#include <QtCore>
#include "X11Capture.h"
  extern "C"
  {
#include <X11/Xatom.h>
#include <X11/cursorfont.h>
#include <X11/X.h>
#include <X11/keysymdef.h>
#include <X11/keysym.h>
#include <X11/Xlib.h>
#include <X11/Xlibint.h>
#include <X11/Xcms.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xrender.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/XShm.h>
#include <sys/ipc.h>
#include <sys/shm.h>
  }

#include <memory>
#include "LockedData.h"
#include "SimpleThread.h"
#include "AVEncoder.h"
#include "Time.h"

namespace recap
{
/*!
  class ScreenCapture
*/
static const int RECAP_FPS = 30;
class ScreenCapture : public QObject
{
    Q_OBJECT
public:
    enum
    {
        /*!
         *   All threads are to pause excecution.
         */
        SC_Stat_Pause = 1,
        /*!
         *   All threads are to proceed excecution as usual.
         */
        SC_Stat_Normal = 0,
        /*!
         *   All threads are to abort all operations as soon as possible,
         *   releasing all resources.
         */
        SC_Stat_Shutdown = -1,
    };


    /*!
      constructor
    */
    ScreenCapture(QObject *parent=0);
    virtual ~ScreenCapture();
    bool isRunning() const;
    
    void setVideoOutputDevice(QIODevice& vod);
public slots:
    virtual void stop();
    virtual void start();

signals:
    void started();
    void stopped();

protected:
    virtual void preStart ();
    virtual void postStart ();
    virtual void preStop ();
    virtual void postStop ();

    LockedData<int> m_status;

private:
    bool m_isRunning;
    Q_INVOKABLE void doCaptureVideo();
    Q_INVOKABLE void doEncode();
    QThread *m_videoCaptureThread;
    QThread *m_encoderThread;
    X11Capture m_x11Capture;
    
    AVEncoder m_encoder;
    
    Time m_startTime;
};
}

#endif //RECAP_SCREENCAPTURE_H

